package handler

import (
	"context"
	"fmt"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"gorm.io/gorm"
	"rpc_demo/user_srv/global"
	"rpc_demo/user_srv/model"
	"rpc_demo/user_srv/proto"
	"time"
)

type UserServer struct {
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}
		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

func ModelToResponse(user model.User) proto.UserInfoResponse {
	userInfoResp := proto.UserInfoResponse{
		Id:       user.ID,
		Password: user.Password,
		NickName: user.NickName,
		Gender:   user.Gender,
		Role:     user.Role,
	}
	if user.Birthday != nil {
		userInfoResp.BirthDay = uint64(user.Birthday.Unix())
	}
	return userInfoResp
}

// 用户列表
func (u *UserServer) GetUserList(ctx context.Context, req *proto.PageInfo) (*proto.UserListResponse, error) {
	fmt.Println(44444444444)
	var users []model.User

	fmt.Println("userLiset,adsfdasfasdfsadf")
	result := global.Db.Scopes(Paginate(int(req.Pn), int(req.Pgize))).Find(&users)
	if result.Error != nil {
		return nil, result.Error
	}
	resp := &proto.UserListResponse{}

	for _, user := range users {
		userInfoResp := ModelToResponse(user)
		resp.Data = append(resp.Data, &userInfoResp)
	}
	resp.Total = uint32(result.RowsAffected)
	return resp, nil

}
func (u *UserServer) GetUserMobile(ctx context.Context, req *proto.UserMobileReq) (*proto.UserInfoResponse, error) {

	var user model.User
	if req.Mobile == "" {
		return nil, status.Errorf(codes.InvalidArgument, "手机号不能为空")
	}
	result := global.Db.Where(&model.User{Mobile: req.Mobile}).First(&user)

	if result.Error != nil {
		return nil, result.Error
	}
	resp := ModelToResponse(user)
	return &resp, nil
}

// 获取用户信息
func (u *UserServer) GetUserId(ctx context.Context, req *proto.IdReq) (*proto.UserInfoResponse, error) {
	var user model.User
	if req.Id <= 0 {
		return nil, status.Errorf(codes.InvalidArgument, "Id不能为空")
	}
	result := global.Db.First(&user, req.Id)
	if result.Error != nil {
		return nil, result.Error
	}
	resp := ModelToResponse(user)

	return &resp, nil
}

// 新建用户
func (u *UserServer) CreateUser(ctx context.Context, req *proto.CreateUserInfo) (*proto.UserInfoResponse, error) {
	var user model.User
	if req.Mobile == "" {
		return nil, status.Errorf(codes.InvalidArgument, "手机号不能为空")
	}
	result := global.Db.Where(&model.User{Mobile: req.Mobile}).First(&user)
	fmt.Println(result.Error)
	//if result.Error != nil {
	//	return nil, status.Errorf(codes.NotFound, "用户信息获取失败")
	//}
	if result.RowsAffected == 1 {
		return nil, status.Errorf(codes.AlreadyExists, "用户信息已存在")
	}

	user.Mobile = req.Mobile
	user.NickName = req.NickName
	user.Password = "3213123"

	tx := global.Db.Create(&user)

	if tx.Error != nil {
		return nil, tx.Error
	}
	resp := ModelToResponse(user)
	return &resp, nil
}

// 更新用户信息
func (u *UserServer) UpdateUser(ctx context.Context, req *proto.UpdateUserInfo) (*emptypb.Empty, error) {
	var user model.User
	if req.Id <= 0 {
		return nil, status.Errorf(codes.InvalidArgument, "Id不能为空")
	}
	result := global.Db.First(&user, req.Id)
	if result.Error != nil {
		return nil, result.Error
	}
	birthDay := time.Unix(int64(req.Birthday), 0)
	user.Mobile = req.Mobile
	user.NickName = req.NickName
	user.Birthday = &birthDay

	res := global.Db.Updates(&user)
	if res.Error != nil {
		return nil, res.Error
	}

	return &emptypb.Empty{}, nil
}
func (u *UserServer) CheckPassword(ctx context.Context, req *proto.CheckPasswordInfo) (*proto.CheckResponse, error) {
	return &proto.CheckResponse{}, nil
}
